var dbConfig = require('../utils/dbconfig.js');
// 获取文章列表
async function getArticleList(req, res, next) {
  console.log(req)
  let sql = 'select article.id,title,article_con as content,introduce,addTime,type,type.typeName from article,type where article.type  = type.id'
  let sqlArr = [];
  let call = (err, data) => {
    if (err) {
      res.status = 401
      res.send({
        msg: '查询失败'
      })
    } else {
      res.status = 200
      res.send({
        data: data,
        status: res.status
      });
    }
  };
  dbConfig.sqlConnect(sql, sqlArr, call);
}

// 根据Id获取文章详情
async function getArticleById(req, res, next) {
  var id = req.query.articleID
  console.log(id)
  const sql = `select article_con,title,addTime from article where id = ${id}`
  let sqlArr = []
  let call = (err, data) => {
    if (err) {
      res.status = 401
      res.send({
        msg: '查询失败'
      })
    } else {
      res.status = 200
      res.send({
        data: data,
        status: res.status
      });
    }
  };
  dbConfig.sqlConnect(sql, sqlArr, call)
}


// 获取主要文章分类
async function getArticleType(req, res, next) {
  const sql = 'select type.id as id,type.typeName as name, type.typeBg as bg from type where type.id !=6 and type.id!=8'
  const callback = (err, data) => {
    if (err) {
      res.send({ err: err })
    } else {
      res.json(
        {
          data
        }
      )
    }
  }
  dbConfig.sqlConnect(sql, [], callback)
}
module.exports = {
  getArticleList,
  getArticleById, getArticleType
};
